package miguel.api;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.util.Log;

import miguel.utils.DownloadHelper;
import miguel.utils.Prefs;

public class UserPermisos {
	
	public static final String UserPermisosUrl = "http://series.ly/scripts/login/login.php";
	public static final String UserPermisosUrlAceptar = "http://series.ly/authUser.php";
	public static final String TAG = "UserPermisos";
	private static class UserPermisosRequest {
		private String lg_login;
		private String lg_pass;
		private String callback_url = "no";
		private String auth_token;
		private String autologin = "1";

		public UserPermisosRequest(String token, String login, String pass) {
			auth_token = token;
			lg_login = login;
			lg_pass = pass;
		}

		public String getUserToken() {
			String userToken = null;
			try {
				DownloadHelper helper = new DownloadHelper();
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("lg_login", lg_login));
				params.add(new BasicNameValuePair("lg_pass", lg_pass));
				params.add(new BasicNameValuePair("callback_url", callback_url));
				params.add(new BasicNameValuePair("auth_token", auth_token));
				params.add(new BasicNameValuePair("autologin", autologin));
				params.add(new BasicNameValuePair("Expect", "100-Continue"));
				userToken = helper.sendPost(UserPermisosUrl, params);				
			} catch (Exception e) {
				Log.e(TAG, "Error: " + e.getLocalizedMessage());
				e.printStackTrace();
			}
			return userToken;
		}
		
		public String getAceptarPermisosUserToken() {
			String userToken = null;
			try {
				DownloadHelper helper = new DownloadHelper();
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("auth_token", auth_token));
				params.add(new BasicNameValuePair("callback_url", callback_url));
				params.add(new BasicNameValuePair("action", "perm"));
				userToken = helper.sendPost(UserPermisosUrlAceptar, params);				
			} catch (Exception e) {
				Log.e(TAG, "Error: " + e.getLocalizedMessage());
				e.printStackTrace();
			}
			return userToken;
		}
	}
	
	public static String getUserToken(Context c) {
		String login = Prefs.getString(c, "lg_login", "");
		String pass = Prefs.getString(c, "lg_pass", "");
		String token = Prefs.getString(c, "auth_token", "");
		UserPermisosRequest request = new UserPermisosRequest(token, login, pass);
		String userToken = request.getUserToken();
		Prefs.put(c, "user_token", userToken);
		return userToken;
	}
	
	public static String aceptarPermisosUserToken(Context c) {
		String login = Prefs.getString(c, "lg_login", "");
		String pass = Prefs.getString(c, "lg_pass", "");
		String token = Prefs.getString(c, "auth_token", "");
		UserPermisosRequest request = new UserPermisosRequest(token, login, pass);
		String userToken = request.getAceptarPermisosUserToken();
		Prefs.put(c, "user_token", userToken);
		return userToken;
	}
}
